import * as actionTypes from './constants'


import { getQrCodeKey, getQrCode, getQrcodeStatus, getLoginStatus, AnonimouseLogin } from '@/services/login'

import { getUserDetail } from "@/services/user"

const changeQrcodeKey = (res) => ({
  type: actionTypes.CHAGNE_QRCODE_KEY,
  qrcode: res.data
})

const changeQrCodeBase = (res) => ({
  type: actionTypes.CHAGNE_QRCODE_BASE,
  qrcode: res
})

const changeQrCodeStatusAction = (res) => ({
  type: actionTypes.CHANGE_QRCODE_STATUS,
  status: res
})

const changeLoginStatus = (res) => ({
  type: actionTypes.CHANGE_LOGIN_STATUS,
  loginStatus: res
})

const changeCookie = (cookie) => ({
  type: actionTypes.CHANGE_LOGIN_COOKIE,
  cookie,
})

const changeUserDetailAction = (detail) => ({
  type: actionTypes.CHANGE_USER_DETAIL,
  detail,
})

export const changeIsLoginAction = (value) => ({
  type: actionTypes.CHANGE_IS_LOGIN,
  value
})

export const changeIsShow = () => ({
  type: actionTypes.CHANGE_LOGIN_ISSHOW
})

export const changUserId = (id) => {
  localStorage.setItem('userId', id)
  return {
    type: actionTypes.CHANGE_USER_ID,
    id
  }
}

export const getQrKey = () => {
  return dispatch => {
    getQrCodeKey().then(res => {
      dispatch(changeQrcodeKey(res))
    })
  }
}

export const getQrcode = (key) => {
  return dispatch => {
    getQrCode(key).then(res => {
      console.log(res);
      dispatch(changeQrCodeBase(res))
    })
  }
}

export const getQrStatus = (key) => {
  return dispatch => {
    getQrcodeStatus(key).then(res => {
      dispatch(changeQrCodeStatusAction(res))
    })
  }
}

export const getCookie = () => {
  return dispatch => {
    AnonimouseLogin().then(res => {
      dispatch(changeCookie(res))
    })
  }
}

export const getLgStatus = (cookie) => {
  console.log('触发,chufa');
  return dispatch => {
    getLoginStatus(cookie).then(res => {
      dispatch(changeIsLoginAction(true))
      getUserDetail(res.data.account.id).then(res => {
        dispatch(changeUserDetailAction(res))
      })
      dispatch(changeLoginStatus(res))
      dispatch(changUserId(res.data.account.id))
    })
  }
}

export const getUserDetailCommit = (uid) => {
  console.log('执行getUserDetailCommit');
  return dispatch => {
    getUserDetail(uid).then(res => {
      dispatch(changeUserDetailAction(res))
    })
  }
}

export const changeIsLogin = (value) => {
  console.log('执行changeIsLogin');
  return dispatch => {
    dispatch(changeIsLoginAction(value))
  }
} 